package com.wxlsoft.excel.convert.handlers;

import java.util.List;

import com.wxlsoft.excel.convert.model.BasePackageRecord;
import com.wxlsoft.excel.convert.model.ServiceBody;
import com.wxlsoft.excel.convert.model.ServiceHead;


/**
 * 报文解析处理
 * @author wangxiaoliang
 */
public class ServicePackageParser {

	/**
	 * 转报文头
	 * @param data
	 * @param rowNum 
	 * @return
	 */
	public static ServiceHead toServiceHead(ServiceHead head, BasePackageRecord data, Integer rowNum) {
		if (rowNum == 0) {
			head.setTradeCode(data.getCnNamel());
			head.setServiceName(data.getCnNamer().replaceAll("(.*):(.*)$", "$2($1)"));
		} else if (rowNum == 1) {
			head.setTradeName(data.getCnNamel());
			head.setServiceOperateName(data.getCnNamer().replaceAll("(.*):(.*)$", "$2($1)"));
		} else if (rowNum == 2) {
			head.setTradeDesc(data.getCnNamel());
			head.setServiceDesc(data.getCnNamer());
		} else if (rowNum == 3) {
			head.setSceneDesc(data.getCnNamer());
		}
		return head;
	}    	
	
	/**
	 * 转输入报文
	 * @param data
	 * @return
	 */
	public static ServiceBody toServiceBody(BasePackageRecord data) {
		ServiceBody input = new ServiceBody();
		input.setEnNamel(data.getEnNamel());
		input.setCnNamel(data.getCnNamel());
		input.setDataTypel(data.getDataTypel());
		input.setDataLengthl(data.getDataLengthl());
		input.setIsNeedInputl(data.getIsNeedInputl());
		input.setRemarkl(data.getRemarkl());
		
		input.setEnNamer(data.getEnNamer());
		input.setCnNamer(data.getCnNamer());
		input.setDataTyper(data.getDataTyper());
		input.setRestraint(data.getRestraint());
		input.setIsNeedInputr(data.getIsNeedInputr());
		input.setRemarkr(data.getRemarkr());
		return input;
	}

	/**
	 * 若模板需要填充，但是没有数据，此时不会出发 RowWriteHandler, 需要主动填充空值
	 * @param outputs
	 * @return
	 */
	public static boolean fillEmptyOutput(List<ServiceBody> outputs) {
		if (outputs.isEmpty()) {
			ServiceBody input = new ServiceBody();
			input.setRed(ServiceEmptyOutsWriteHandler.DELETESTRING);
			outputs.add(input);
			return true;
		}
		return false;
	}
	
}

